package com.tvunetworks.center.user.controller.api;

import com.github.pagehelper.PageInfo;
import com.tvunetworks.center.common.constant.LocalConstant;
import com.tvunetworks.center.common.model.LoginUser;
import com.tvunetworks.center.common.model.SearchDeviceParam;
import com.tvunetworks.center.common.util.*;
import com.tvunetworks.center.user.annotation.Logined;
import com.tvunetworks.center.user.model.param.PageDeviceGeoParam;
import com.tvunetworks.center.user.model.vo.DeviceGeoVo;
import com.tvunetworks.center.user.service.DeviceGeoService;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: tvucc-aggregator
 * @description: UserFeatureApiController
 * @author lebronchen
 * @create: 2019-06-21 14:22
 **/
@RestController
@RequestMapping("/api/userDevice")
public class UserDeviceApiController {

    @Autowired
    private DeviceGeoService deviceGeoService;

    /**
     *
     * @param loginUser loginUser
     * @param param param
     * @param deviceGeoParam deviceGeoParam
     * @param pageNum pageNum
     * @param pageSize pageSize
     * @return Result
     */
    @GetMapping("/getUserDevice")
    public Result<Object>  getUserDevice(@Logined LoginUser loginUser, SearchDeviceParam param, PageDeviceGeoParam deviceGeoParam, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam Integer pageSize){
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        if(pageSize == null){
            pageSize = 99999999;
        }
        PageInfo<DeviceGeoVo> deviceInfo = deviceGeoService.pageDeviceGeo(loginUser, param, deviceGeoParam, pageNum, pageSize, Boolean.FALSE);
        return ResultUtil.success(deviceInfo);
    }

    /**
     *
     * @param userId userId
     * @param fromDeviceId fromDeviceId
     * @param toDeviecID toDeviecID
     * @return Result
     */
    @GetMapping("/copyDeviceForUser")
    public Result<Object>  copyDeviceForUser(@RequestParam String userId, @RequestParam String fromDeviceId, @RequestParam String toDeviecID){
//        userFeatureService.apiUpdateUserRole(userId, feature, enable);
        return ResultUtil.success();
    }

    /**
     *
     * @param userId userId
     * @param deviceId deviceId
     * @return Result
     */
    @GetMapping("/removeDeviceFromUser")
    public Result<Object>  removeDeviceFromUser(@RequestParam String userId, @RequestParam String deviceId){
//        userFeatureService.apiUpdateUserRole(userId, feature, enable);
        return ResultUtil.success();
    }

    /**
     *
     * @param args args
     * @throws IOException 编译异常
     */
    public static void main(String[] args) throws IOException {
        RestTemplate restTemplate = new RestTemplate();

        String url = "http://10.12.22.101:7150/uniformApi/demo";

        long timestamp = System.currentTimeMillis();
// 全局唯一的一个key，会配置在服务器和所有客户端（Device）
        String uniformJWTKet = "8a8c96f95b19ca27015b19ddca0e0013";
// 这个key是JWT加密解密的key
        String key = EndecryptUtil.MD5(timestamp + uniformJWTKet);

// 封装RequestBody
        Map<String, Object> map = new HashMap<>();
        map.put("peerId", "a30e177a060069d3");
        String param = JwtUtil.createJWT(map, key);
// Http 请求的 request body
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("param", param);

// 封装Http header
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);

        Map<String, Object> headerMap = new HashMap<>();
        headerMap.put("requestId", MyUtil.getUUID());
        headerMap.put("timestamp", timestamp + "");
        String headerParam = JsonUtil.toJsonStr(headerMap);
        httpHeaders.add(LocalConstant.JWT_API_HEADER_PARAM_KEY, headerParam);

// 发送Http请求
        HttpEntity httpEntity = new HttpEntity(paramMap, null);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);

        int httpStatus = responseEntity.getStatusCode().value();
        if (httpStatus == 200) {
// 服务端返回值
            String body = responseEntity.getBody();
            if (StringUtils.contains(body, "errorCode")) {
                System.out.println("*******************");
                System.out.println(body);
                return;
            }
// 解密返回值
            Claims claims = JwtUtil.parseJWT(body, key);
// 解密客户端返回值
            String result = JsonUtil.toJsonStr(claims);
            System.out.println(result);
        }
    }


}
